package cn.com.tianpeidong.restaurantadmin.project.admin.dishestype.rest;

import cn.com.tianpeidong.restaurantadmin.common.util.page.PageWrapper;
import cn.com.tianpeidong.restaurantadmin.project.admin.dishestype.entity.DishesType;
import cn.com.tianpeidong.restaurantadmin.project.admin.dishestype.service.IDishesTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import javax.validation.Valid;
import java.net.URI;
import java.util.List;

/**
 * @author tianpeidong
 * @version 创建时间: 19-3-26 下午9:36
 */
@RestController
@RequestMapping("/dishesType")
public class TypeController {

    @Autowired
    private IDishesTypeService dishesTypeService;

    @RequiresPermissions("dishesType:list")
    @GetMapping()
    public ResponseEntity list(
            @RequestParam(value = "per_page", required = false) Integer pageSize,
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam("pagi") boolean isPagi) {
        if (!isPagi) {
            return this.listDishesType();
        }

        PageWrapper pageWrapper = new PageWrapper(pageSize, page);
        List<DishesType> list = dishesTypeService.listDishesType(new DishesType());
        return ResponseEntity.ok(pageWrapper.getResult(list));
    }

    @RequiresPermissions("dishesType:list")
    @PostMapping("/search")
    public ResponseEntity search(
            @RequestParam(value = "per_page", required = false) Integer pageSize,
            @RequestParam(value = "page", required = false) Integer page,
            @RequestBody DishesType dishesTypeVo) {
        PageWrapper pageWrapper = new PageWrapper(pageSize, page);
        List<DishesType> list = dishesTypeService.listDishesType(dishesTypeVo);
        return ResponseEntity.ok(pageWrapper.getResult(list));
    }

    @GetMapping("/{id}")
    @RequiresPermissions("dishesType:list")
    public ResponseEntity getDishesTypeById(@PathVariable("id") Long id) {
        DishesType dishesType = dishesTypeService.getDishesTypeById(id);
        return ResponseEntity.ok(dishesType);
    }

    @RequiresPermissions("dishesType:add")
    @PostMapping()
    public ResponseEntity add(@Valid @RequestBody DishesType dishesType) {
        DishesType res = dishesTypeService.addDishesType(dishesType);
        URI location = ServletUriComponentsBuilder.fromCurrentRequest()
                .path("/{id}")
                .buildAndExpand(res.getId())
                .toUri();
        return ResponseEntity.created(location).build();
    }

    @RequiresPermissions("dishesType:edit")
    @PutMapping()
    public ResponseEntity edit(@Valid @RequestBody DishesType dishesType) {
        DishesType res = dishesTypeService.editDishesType(dishesType);
        URI location = ServletUriComponentsBuilder.fromCurrentRequest()
                .path("/{id}")
                .buildAndExpand(res.getId())
                .toUri();
        return ResponseEntity.created(location).build();
    }

    @RequiresPermissions("dishesType:delete")
    @DeleteMapping("/{id}")
    public ResponseEntity delete(@PathVariable("id") Long id) {
        dishesTypeService.deleteDishesType(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 列出系统已有菜品类型
     * @return
     */
    private ResponseEntity listDishesType() {
        List<DishesType> types = dishesTypeService.listDishesType(new DishesType());
        return ResponseEntity.ok(types);
    }
}
